import Vue from 'vue'
import { GetterTree, MutationTree, ActionTree, Module, Commit } from 'vuex'

import * as http from '@/http/cms/admin/permission'
import { arrayFindIndex } from '@/utils'

// 接口
export interface State {
  currentPage: number,
  perPage: number,
  total: number,
  permissions: Array<any>,
  permission: any,
  groups: Array<any>
}

// 状态
const state: State = {
  currentPage: 1,
  perPage: 20,
  total: 0,
  permissions: [],
  permission: {},
  groups: []
}

// 获取
const getters: GetterTree<State, any> = {}

// 改变
const mutations: MutationTree<State> = {
  /**
   * 分页
   *
   * @param {object} state
   * @param {object} data
   */
  page(state: State, {
    data_list,
    current_page,
    per_page,
    total
  }: { data_list: any, current_page: number, per_page: number, total: number }): void {
    state.permissions = data_list
    state.currentPage = current_page
    state.perPage = per_page
    state.total = total
  },
  
  /**
   * 添加行
   *
   * @param {object} state
   * @param {object} data
   */
  store(state: State, data: any): void {
    if (state.permissions.length === 0) {
      state.permissions = [data]
    } else {
      state.permissions.unshift(data)
    }
  },
  
  /**
   * 显示
   *
   * @param state
   * @param data
   */
  show(state: State, data: any): void {
    state.permission = data
  },
  
  /**
   * 组
   *
   * @param {object} state
   * @param {object} data
   */
  groups(state: State, data: any): void {
    state.groups = data
  },
  
  /**
   * 存储新的组
   *
   * @param {object} state
   * @param {object} data
   */
  storeGroups(state: State, data: any): void {
    if (state.groups.length === 0) {
      state.groups = [data]
    } else {
      state.groups.push(data)
    }
  },
  
  /**
   * 更新行
   *
   * @param {object} state
   * @param {int} index
   * @param {object} permission
   * @param {boolean|object} group
   */
  update(state: State, {index, permission, group}: { index: number, permission: any, group: any }): void {
    Vue.set(state.permissions, index, permission)
    if (group && permission.parent_id === 0) {
      const index = arrayFindIndex(state.groups, 'id', group.id)
      if (index >= 0) {
        Vue.set(state.groups, index, group)
      }
    }
  },
  
  /**
   * 删除行
   *
   * @param {object} state
   * @param {int} index
   */
  destroy(state: State, index: number): void {
    state.permissions.splice(index, 1)
  }
}

// 方法
const actions: ActionTree<State, any> = {
  /**
   * 首页
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  index({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.index(payload.param).then((res) => {
      if (res.status === 200) {
        commit('groups', res.data.groups)
        commit('page', res.data)
      }
      
      return res
    })
  },
  
  /**
   * 查询
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  query({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.query(payload.param).then(res => {
      if (res.status === 200) {
        commit('page', res.data)
      }
      
      return res
    })
  },
  
  /**
   * 分组
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  group({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.group(payload.param).then(res => {
      if (res.status === 200) {
        commit('groups', res.data)
      }
      
      return res
    })
  },
  
  /**
   * 存储
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  store({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.store(payload.param).then(res => {
      if (res.status === 200) {
        commit('store', res.data.permission)
        
        // 添加到组
        if (res.data.group) {
          commit('storeGroups', res.data.group)
        }
      }
      
      return res
    })
  },
  
  /**
   * 详情
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  show({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.show(payload.id, {}).then(res => {
      if (res.status === 200) {
        commit('show', res.data)
      }
      return res
    })
  },
  
  /**
   * 更新
   *
   * @param commit
   * @param id
   * @param payload
   * @return {Promise<any>}
   */
  update({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.update(payload.id, payload.param).then(res => {
      if (res.status === 200 && res.data) {
        commit('update', {
          index: payload.index,
          permission: res.data.permission,
          group: res.data.group
        })
      }
      
      return res
    })
  },
  
  /**
   * 删除
   *
   * @param commit
   * @param id
   * @param payload
   * @return {Promise<*>}
   */
  destroy({commit}: { commit: Commit }, payload: any) {
    return http.destroy(payload.id, {}).then(res => {
      if (res.status === 200) {
        commit('destroy', payload.index)
      }
      
      return res
    })
  }
}

// 模型
export const permission: Module<State, any> = {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}

export default {
  permission
}

